package com.cly.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AliPayAutoConfiguration {

    @Autowired
    private AliProperties aliProperties;

    public AliPayAutoConfiguration(){
        //初始化ali的配置文件
        Configs.init("zfbinfo.properties");
    }

    /**
     * 创建支付客户端 放入容器
     * @return
     */
    @Bean
    public AlipayTradeService alipayTradeService() {
        return new AlipayTradeServiceImpl.ClientBuilder().build();
    }

    @Bean
    public AlipayClient alipayClient(){
        AlipayClient alipayClient = new DefaultAlipayClient(
                aliProperties.getAliGateway(),
                aliProperties.getAppId(),
                aliProperties.getAliPrivateKey(),
                "json",
                "UTF-8",
                aliProperties.getAliPublicKey(),
                //获得初始化的AlipayClient
                aliProperties.getSignType());
        return alipayClient;
    }

}
